package com.laomuji.android.mvvm.utils

import android.annotation.SuppressLint
import android.graphics.drawable.Drawable
import android.os.Handler
import android.os.Looper
import android.view.MotionEvent
import android.view.View
import com.laomuji.android.mvvm.R


/**
 * 某些情况 除了点击事件 其他被拦截,所以有个恢复时间
 * @author laomuji666
 * @since 2024/2/21
 */
class ViewClickUtil @SuppressLint("ClickableViewAccessibility") constructor(private var onTouchChangedListener: OnTouchChangedListener) {
    private val delayMillis: Long = 400

    private var resumeTime: Long = 0
    private val handler = Handler(Looper.getMainLooper())

    init {
        if (onTouchChangedListener.view != null) {
            onTouchChangedListener.view!!.setOnTouchListener { _: View?, event: MotionEvent ->
                dispatchTouchEvent(event)
                false
            }
        }
    }

    private fun dispatchTouchEvent(ev: MotionEvent) {
        if (ev.action == MotionEvent.ACTION_DOWN) {
            resetTime()
            onTouchChangedListener.onDown()
        }
        if (ev.action == MotionEvent.ACTION_UP || ev.action == MotionEvent.ACTION_CANCEL) {
            handler.removeCallbacksAndMessages(null)
            onTouchChangedListener.onUp()
        }
    }

    private fun resetTime() {
        resumeTime = System.currentTimeMillis() + delayMillis
        handler.postDelayed({
            if (System.currentTimeMillis() > resumeTime) {
                onTouchChangedListener.onUp()
                handler.removeCallbacksAndMessages(null)
            }
        }, delayMillis + 100)
    }

    abstract class OnTouchChangedListener(val view: View?) {
        fun onDown() {
            if (view == null) {
                return
            }
            if (saveDrawable == null) {
                saveDrawable = view.background
            }
            view.background = this.roundClickDrawable
        }

        fun onUp() {
            if (view == null) {
                return
            }
            view.background = saveDrawable
        }

        /**
         * 提供保存Drawable的功能
         */
        protected open var saveDrawable: Drawable? = null

        @get:Synchronized
        @get:SuppressLint("UseCompatLoadingForDrawables")
        val roundClickDrawable: Drawable?
            get() {
                if (Companion.roundClickDrawable == null) {
                    Companion.roundClickDrawable = PackageUtil.getApplicationContext().getDrawable(R.drawable.bg_radius_round_click)
                }
                return Companion.roundClickDrawable
            }

        @get:Synchronized
        @get:SuppressLint("UseCompatLoadingForDrawables")
        val rectClickDrawable: Drawable?
            get() {
                if (Companion.rectClickDrawable == null) {
                    Companion.rectClickDrawable = PackageUtil.getApplicationContext().getDrawable(R.drawable.bg_radius_rect_click)
                }
                return Companion.rectClickDrawable
            }

        companion object {
            /**
             * 圆形点击
             */
            private var roundClickDrawable: Drawable? = null

            /**
             * 矩形点击
             */
            private var rectClickDrawable: Drawable? = null
        }
    }


}